package chen.yi.component.xss;

import chen.yi.component.xss.constant.WhiteConstant;
import chen.yi.component.xss.filter.XssFilter;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * @author huat
 **/
@Configuration
public class XssApplication {

    @Value("${chen-yi.xss.white.url}")
    private String xssWhiteList;
    @Bean
    public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns("/*");
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        if(StrUtil.isNotBlank(xssWhiteList)){
            registration.addInitParameter(WhiteConstant.WHITE_KEY, xssWhiteList);
        }
        return registration;
    }
}
